//引入express第三方模块
const express = require('express')
//引入body-parser中间件
const bodyParser = require('body-parser');
//引入自带path模块
const path = require('path');
//引用cookie-parser
const cookieParase = require('cookie-parser');
//引入session
const session = require('express-session');
//引入user路由
const userRouter = require('./routes/user.js');
//引入teacher路由
const teacherRouter = require('./routes/teacher.js')
//引入student路由
const studentRouter = require('./routes/student.js');
//创建web服务器
const app = express();
//设置端口
app.listen(8080, () => {
	console.log('服务器启动成功,浏览器打开:localhost:8080')
})
//使用cookie-parser
app.use(cookieParase());
app.use(session({
	secret: 'keyboard cat',
	resave: false,
	saveUninitialized: true
}))

//托管静态资源
app.use(express.static(path.join(__dirname, '/public')))

//使用body-parser中间件将所有post请求的数据解析为对象,放在路由前边
app.use(bodyParser.urlencoded({ //也可使用内置express.urlencoded
	limit: '50mb',
	extended: false //不使用第三方查询字符串模块，就会使用核心模块querystring
}));

// 配置跨域
app.all('*', function (req, res, next) {
  //允许接收不同协议，不同端口的请求
  res.header('Access-Control-Allow-Origin', '*');
  //不同方式（接收任意的请求方式，默认情况下只接收get和post）
  res.header('Access-Control-Allow-Methods', '*');
    // 请求头  x-token
  res.header("Access-Control-Allow-Headers", "*");
  next();
});

// 安装: npm  i  -save  cors// 在nodejs express项目的app.js中: //     var cors=require(“cors”); //先引入cors中间件//     var app=express();//     app.use(cors({ //添加允许跨域的客户端地址//       origin:[ "http://127.0.0.1:5500",... ]//     }))

//挂载路由器到web服务器
app.use('/v1/users', userRouter)
app.use('/v1/students', studentRouter)
app.use('/v1/teachers', teacherRouter)

//参数里为'/'则是默认打开页面
app.get('/', (req, res) => {
	res.redirect('/login');
})

//登陆界面接口
app.get('/login', (req, res) => {


	if (req.cookies.userinfo == undefined) {
		res.sendFile(path.join(__dirname, "/public/login.html"))
		return;
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		0) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/homePage.html"))
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		1) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/teacher.html"))
	}

})

//主界面接口
app.get('/homePage', (req, res) => {
	//console.log(req.cookies.userinfo)
	if (req.cookies.userinfo == undefined) {
		res.redirect('/');
		return;
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		0) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/homePage.html"))
	}


})

//教师界面接口
app.get('/teacher', (req, res) => {
	//console.log(req.cookies.userinfo)
	if (req.cookies.userinfo1 == undefined) {
		res.redirect('/');
		return;
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		1) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/teacher.html"))
	}

})


//更新头像界面接口1
app.get('/updateicon', (req, res) => {
	//console.log(req.cookies.userinfo)
	if (req.cookies.userinfo == undefined) {
		res.redirect('/');
		return;
	} else if (req.cookies.userinfo.username == req.session.username) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/updateicon.html"))
	}

})

//更新头像界面接口2
app.get('/updateicon2', (req, res) => {
	//console.log(req.cookies.userinfo)
	if (req.cookies.userinfo == undefined) {
		res.redirect('/');
		return;
	} else if (req.cookies.userinfo.username == req.session.username) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/updateicon2.html"))
	}

})





//找回密码界面接口
app.get('/forget', (req, res) => {
	if (req.cookies.userinfo == undefined) {
		res.sendFile(path.join(__dirname, "/public/forget.html"))
		return;
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		0) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/homePage.html"))
	} else if (req.cookies.userinfo.username == req.session.username && req.cookies.userinfo.type ==
		1) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/teacher.html"))
	}

})

//修改密码界面
app.get('/updatepassword', (req, res) => {
	//console.log(req.cookies.userinfo)
	if (req.cookies.userinfo == undefined) {
		res.redirect('/');
		return;
	} else if (req.cookies.userinfo.username == req.session.username) { //cookie中存在用户信息，则直接进入主页面
		//console.log(req.cookies.userinfo)
		res.sendFile(path.join(__dirname, "/public/changepassword.html"))
	}
})



//404界面,如果路由不存在则返回404页面
app.get('*', (req, res) => {
	res.sendFile(path.join(__dirname, "/public/404.html"))
})


//错误处理中间件
app.use((err, req, res, next) => {
	//err接收到的路由中的错误
	console.log(err);
	//响应状态码 500
	res.status(500).send({
		code: 500,
		msg: err.sqlMessage
	})
})
